from .. import *
from ..models import psn
from ..models import psn_resume
from ..models import psn_resume_project_exprience
from ..models import psn_resume_work_exprience
from ..models import psn_resume_edu_exprience


# 个人用户注册
@csrf_exempt
def psnSubmitUser(request):
    message = json.loads(request.body)

    if psn.objects.filter(username=message['username']):
        return ErrResponse('用户名已存在')
    if psn.objects.filter(email=message['email']):
        return ErrResponse('邮箱已存在')
    if psn.objects.filter(tel=message['tel']):
        return ErrResponse('手机号已存在')

    saveNewpsnId = getId()
    saveNewPsn = psn(psnId=saveNewpsnId, username=message['username'], password=message['password'],
                     email=message['email'], tel=message['tel'])
    if saveNewPsn.save():
        return ErrResponse('注册失败')
    # 注册个人用户的同时创建一份简历
    saveNewPsnResumeId = getId()
    saveNewPsnResume = psn_resume(
        resumeId=saveNewPsnResumeId, psnId=saveNewpsnId, email=message['email'], tel=message['tel'])
    saveNewPsnResume.save()
    request.session['psnId'] = saveNewpsnId
    return Response(msg=saveNewpsnId)


# 个人登录
@csrf_exempt
def psnLogin(request):
    message = json.loads(request.body)
    username = message['username']
    password = message['password']
    try:
        m = psn.objects.get(username=username)
    except psn.DoesNotExist:
        return AccountNotFoundResponse()
    if m.password == password:
        request.session['psnId'] = m.psnId
        return Response(msg=m.psnId)
    return LoginErrorResponse()


# 查询简历信息
@csrf_exempt
def getPsnResumeInfo(request):
    message = json.loads(request.body)
    data = {}
    psnId = message['psnId']
    resumeId = psn_resume.objects.filter(psnId=psnId)[0].resumeId
    data['psnBaseInfo'] = serializers.serialize(
        'json', psn_resume.objects.order_by("updateTime").filter(psnId=psnId))
    data['psnProjectInfo'] = serializers.serialize(
        'json', psn_resume_project_exprience.objects.order_by("startTime").filter(resumeId=resumeId))
    data['psnWorkInfo'] = serializers.serialize(
        'json', psn_resume_work_exprience.objects.order_by("startTime").filter(resumeId=resumeId))
    data['psnEduInfo'] = serializers.serialize(
        'json', psn_resume_edu_exprience.objects.order_by("gradTime").filter(resumeId=resumeId))
    return Response(msg=json.dumps(data))


# 保存个人基本信息
@csrf_exempt
def savePsnBaseInfo(request):
    message = json.loads(request.body)
    psnId = message['psnId']
    m = message['msg']
    updatePsnBaseInfo = psn_resume.objects.filter(psnId=psnId).update(name=m['name'], age=m['age'], sex=m['sex'], location=m['location'], workExp=m['workExp'], jobName=m['jobName'], jobPay=m['jobPay'],
                                                                      jobType=m['jobType'], jobAdd=m['jobAdd'], tel=m['tel'], email=m['email'], nowStatus=m['nowStatus'], selfDisp=m['selfDisp'], psnTag=m['psnTag'], degree=m['degree'], updateTime=datetime.date.today())
    if updatePsnBaseInfo:
        return Response(msg='保存成功')
    return Response(msg='保存失败')


# 保存个人项目信息
@csrf_exempt
def savePsnProjectInfo(request):
    message = json.loads(request.body)
    m = message['msg']
    projectId = message['itemId']
    if isErrorId(projectId):
        projectId = getId()
    resumeId = psn_resume.objects.filter(psnId=message['psnId'])[0].resumeId
    psn_resume_project_exprience(
        projectId=projectId,
        resumeId=resumeId,
        projectName=m['projectName'],
        jobName=m['jobName'],
        orgName=m['orgName'],
        startTime=m['startTime'],
        endTime=m['endTime'],
        tecName=m['tecName'],
        projectDisp=m['projectDisp']).save()
    data = serializers.serialize('json', psn_resume_project_exprience.objects.order_by(
        "startTime").filter(resumeId=resumeId).all())
    return Response(msg=data)


# 删除项目信息
@csrf_exempt
def delPsnProjectInfo(request):
    message = json.loads(request.body)
    psn_resume_project_exprience.objects.get(
        projectId=message['msg']).delete()
    return Response(msg='')


# 保存工作经历信息
@csrf_exempt
def savePsnWorkInfo(request):
    message = json.loads(request.body)
    m = message['msg']
    workId = message['itemId']
    if isErrorId(workId):
        workId = getId()
    resumeId = psn_resume.objects.filter(psnId=message['psnId'])[0].resumeId

    psn_resume_work_exprience(
        workId=workId, resumeId=resumeId, companyName=m['companyName'], jobName=m['jobName'], startTime=m['startTime'], endTime=m['endTime'], workDisp=m['workDisp']).save()
    data = serializers.serialize('json', psn_resume_work_exprience.objects.order_by(
        "startTime").filter(resumeId=resumeId).all())
    return Response(msg=data)


# 删除工作经历信息
@csrf_exempt
def delPsnWorkInfo(request):
    message = json.loads(request.body)
    psn_resume_work_exprience.objects.get(
        workId=message['msg']).delete()
    return Response(msg='')


# 保存教育经历信息
@csrf_exempt
def savePsnEduInfo(request):
    message = json.loads(request.body)
    m = message['msg']
    eduId = message['itemId']
    if isErrorId(eduId):
        eduId = getId()
    resumeId = psn_resume.objects.filter(
        psnId=message['psnId'])[0].resumeId

    psn_resume_edu_exprience(
        eduId=eduId, resumeId=resumeId, schoolName=m['schoolName'], majorName=m['majorName'], gradTime=m['gradTime'], degree=m['degree']).save()
    data = serializers.serialize('json', psn_resume_edu_exprience.objects.order_by(
        "gradTime").filter(resumeId=resumeId).all())
    return Response(msg=json.dumps(data))


# 删除教育经历信息
@csrf_exempt
def delPsnEduInfo(request):
    message = json.loads(request.body)
    psn_resume_edu_exprience.objects.get(
        eduId=message['msg']).delete()
    return Response(msg='')
